import {
	config
} from '../config.js'
import self from "../main.js"
var baseUrl = '/api';
//#ifdef APP-PLUS
if (process.env.NODE_ENV === 'development') {
	//baseUrl = 'http://172.19.178.5:8084/api';
	baseUrl = 'http://192.168.1.170:8085/api';
} else {
	baseUrl = 'http://42.192.83.226:8085/api';
}
//#endif
var loadingTimes = 0;
export const api = {
	get(config) {
		//loading默认为true
		if (config.loading == undefined || config.loading == null) config.loading = true;
		var promise = new Promise((resolve, reject) => {
			if (config.loading) {
				if (loadingTimes === 0) {
					uni.showLoading({
						mask: true
					});
				}
				loadingTimes++;
			}
			uni.request({
				url: baseUrl + config.url,
				data: config.data,
				method: "GET",
				header: {
					"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
					"Authorization": uni.getStorageSync("authorization")
				},
				dataType: 'json',
				success: (res) => {
					if (res.data.code == '401') {
						//未登录
						uni.removeStorageSync("authorization");
						self.$toast('登录已过期，请重新登录');
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}
					resolve(res.data)
				},
				fail: (res) => {
					self.$toast(res.errMsg);
					reject(res)
				},
				complete: () => {
					if (!config.loading) return;
					loadingTimes--;
					if (loadingTimes === 0) uni.hideLoading();
				}
			});
		})
		return promise;
	},

	post(config) {
		//loading默认为true
		if (config.loading == undefined || config.loading == null) config.loading = true;
		var promise = new Promise((resolve, reject) => {
			if (config.loading) {
				if (loadingTimes === 0) {
					uni.showLoading({
						mask: true
					});
				}
				loadingTimes++;
			}
			if (config.params != undefined && config.params != null) {
				config.url += "?" + Object.keys(config.params).map(key => encodeURIComponent(key) + "=" +
					encodeURIComponent(config.params[key])).join("&");
			}
			uni.request({
				url: baseUrl + config.url,
				data: config.data,
				method: "POST",
				header: {
					'Content-Type': 'application/json; charset=UTF-8',
					"Authorization": uni.getStorageSync("authorization")
				},
				dataType: 'json',
				success: (res) => {
					if (config.url.endsWith("/login") && res.data.code == "200") {
						if (res.header.Authorization == undefined) {
							uni.setStorageSync("authorization", res.header.authorization);
						} else {
							uni.setStorageSync("authorization", res.header.Authorization);
						}
					}
					if (res.data.code == '401') {
						//未登录
						uni.removeStorageSync("authorization");
						self.$toast('登录已过期，请重新登录');
						uni.reLaunch({
							url: "/pages/index/index"
						})
					}
					resolve(res.data)
				},
				fail: (res) => {
					self.$toast(res.errMsg);
					reject(res)
				},
				complete: () => {
					if (!config.loading) return;
					loadingTimes--;
					if (loadingTimes === 0) uni.hideLoading();
				}
			});
		});
		return promise;
	},

	upload(config) {
		//loading默认为true
		if (config.loading == undefined || config.loading == null) config.loading = true;
		var promise = new Promise((resolve, reject) => {
			if (config.loading) {
				if (loadingTimes === 0) {
					uni.showLoading({
						mask: true
					});
				}
				loadingTimes++;
			}
			uni.uploadFile({
				url: baseUrl + config.url, //仅为示例，非真实的接口地址
				filePath: config.file,
				name: 'file',
				formData: config.data,
				header: {
					"Authorization": uni.getStorageSync("authorization")
				},
				success: (res) => {
					resolve(JSON.parse(res.data))
				},
				fail: (res) => {
					self.$toast(res.errMsg);
					reject(res)
				},
				complete: () => {
					if (!config.loading) return;
					loadingTimes--;
					if (loadingTimes === 0) uni.hideLoading();
				}
			});
		});
		return promise;
	}
};
